package main.Q2101_2200;

import java.util.HashMap;
import java.util.Map;

public class Q2131_2140 {
    public static void main(String[] args) {
        System.out.println("Question2131：连接两字母单词得到的最长回文串");
        System.out.println("Question2132：");
        System.out.println("Question2133：");
        System.out.println("Question2134：");
        System.out.println("Question2135：");
        System.out.println("Question2136：");
        System.out.println("Question2137：");
        System.out.println("Question2138：");
        System.out.println("Question2139：");
        System.out.println("Question2140：");
    }
}
class Question2131{
    public int longestPalindrome(String[] words) {
        int maxLength=0;
        boolean flag=false;
        Map<String,Integer> map=new HashMap<>();
        for (String word:words) map.put(word,map.getOrDefault(word,0)+1);
        for (String key:map.keySet()){
            String reverse=new StringBuffer(key).reverse().toString();//回文串单词对称
            if (key.equals(reverse)){
                if (map.get(key)%2==0) maxLength+=map.get(key)*2;
                else {
                    if (map.get(key)==1&&!flag) {
                        maxLength+=2;
                        flag=true;
                    }else if (map.get(key)>1) {
                        maxLength+=(map.get(key)-1)*2;
                        if (flag==false) {
                            maxLength+=2;
                            flag=true;
                        }
                    }
                }
            }
            else if (map.containsKey(reverse)&&!key.contains(reverse)) maxLength+=2*Math.min(map.get(key),map.get(reverse));
        }
        return maxLength;
    }
}